package zhuanyeshixi2.basic.mapper;

import org.apache.ibatis.annotations.*;
import zhuanyeshixi2.basic.domain.User;
import zhuanyeshixi2.basic.vo.UserVo;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    // ================= 原有方法 =================
    @Insert("INSERT INTO users (username, password, real_name, email, phone) VALUES (#{username}, #{password}, #{realName}, #{email}, #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    void insertUser(UserVo userVo);

    @Update("UPDATE users SET password = #{password}, real_name = #{realName}, email = #{email}, phone = #{phone} WHERE username = #{username}")
    void updateUser(UserVo userVo);

    @Delete("DELETE FROM users WHERE username = #{username}")
    int deleteUser(String username);

    @Select("SELECT * FROM users WHERE username = #{username}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    User selectUserWithRoles(String username);

    @Select("SELECT r.role_name FROM roles r JOIN user_roles ur ON r.role_id = ur.role_id JOIN users u ON ur.user_id = u.user_id WHERE u.username = #{username}")
    List<String> selectRolesByUsername(String username);

    @Delete("DELETE FROM user_roles WHERE user_id = (SELECT user_id FROM users WHERE username = #{username})")
    void deleteUserRoles(String username);

    // 根据用户ID插入角色
    @Insert({
            "<script>",
            "INSERT INTO user_roles (user_id, role_id) VALUES ",
            "<foreach collection='roleIds' item='roleId' separator=','>",
            "(#{userId}, #{roleId})",
            "</foreach>",
            "</script>"
    })
    void insertUserRolesByUserId(@Param("userId") Integer userId, @Param("roleIds") List<Integer> roleIds);

    // 根据用户名获取用户ID
    @Select("SELECT user_id FROM users WHERE username = #{username}")
    Integer getUserIdByUsername(String username);

    // ================= 新增必要的方法 =================
    // 根据用户名和密码查询用户
    @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    User selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    // 查询所有用户
    @Select("SELECT * FROM users")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at")
    })
    List<User> selectAllUsers();

    // ================= XML 中定义的方法 =================
    // 删除重复的注解方法，保留XML配置
    User selectByUsername(String username);
    User selectByPrimaryKey(Integer userId);
    List<User> selectByExample(Object example);
    int insert(User user);
    int insertSelective(User user);
    int updateByPrimaryKey(User user);
    int updateByPrimaryKeySelective(User user);
    int deleteByPrimaryKey(Integer userId);

    // ================= XML 中新增的方法 =================
    User selectByEmail(String email);
    List<User> selectSimpleUserList();
    int batchInsert(List<User> list);
    int batchUpdateStatus(Map<String, Object> map);
    int checkUsernameExists(String username);
    int checkEmailExists(String email);
    List<User> selectByPage(Map<String, Object> map);
    Long countByConditions(Map<String, Object> map);
    int batchDelete(List<Integer> list);
    int deleteByExample(Object example);
    Long countByExample(Object example);
    int updateByExampleSelective(Map<String, Object> map);
    int updateByExample(Map<String, Object> map);
}